package cn.zijitc.util;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Base64;

/**
 * @author Mr.li
 * @date 2021/5/31 17:02
 */
public class DingDingUtil {
    public static void pushDingDingTalk(String accessToken,String message,String whois){
        try {
            String sign=appendSign();
            String dingdingUrl="https://oapi.dingtalk.com/robot/send?access_token="+accessToken;
            String url=dingdingUrl+sign;
            DingTalkClient client = new DefaultDingTalkClient(url);
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("text");
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent("输入内容");
            request.setText(text);
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            at.setAtMobiles(Arrays.asList("13263125227"));
            request.setAt(at);

            request.setMsgtype("link");
            OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
            link.setMessageUrl("https://www.dingtalk.com/");
            link.setPicUrl("");
            link.setTitle("输入标题");
            link.setText("");
            request.setLink(link);
            // 设置信息类型
            request.setMsgtype("markdown");
            OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
            markdown.setTitle(whois);
            markdown.setText(","+message);
            request.setMarkdown(markdown);
            OapiRobotSendResponse response = client.execute(request);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    public  void markdown(String message,String token,String whois,String phone){
        try {
            String sign=appendSign();
            String dingdingUrl="https://oapi.dingtalk.com/robot/send?access_token="+token;
            String url=dingdingUrl+sign;
            DingTalkClient client = new DefaultDingTalkClient(url);
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("markdown");
            OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
            markdown.setTitle(whois);
            markdown.setText(phone+message);
            request.setMarkdown(markdown);
            OapiRobotSendResponse response = client.execute(request);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    public void link(String message,String token,String murl,String whois,String picurl){
        try {
            String sign=appendSign();
            String dingdingUrl="https://oapi.dingtalk.com/robot/send?access_token="+token;
            String url=dingdingUrl+sign;
            DingTalkClient client = new DefaultDingTalkClient(url);
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("link");
            OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
            link.setMessageUrl(murl);
            link.setPicUrl(picurl);
            link.setTitle(whois);
            link.setText(message);
            request.setLink(link);
            OapiRobotSendResponse response = client.execute(request);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    public void text(String message,String token,String phone,String qun,String name){
        try {
            String sign=appendSign();
            String dingdingUrl="https://oapi.dingtalk.com/robot/send?access_token="+token;
            String url=dingdingUrl+sign;
            DingTalkClient client = new DefaultDingTalkClient(url);
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("text");
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent(message);
            request.setText(text);
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            at.setAtMobiles(Arrays.asList(phone));
            if (name!=null) {
                at.setAtUserIds(Arrays.asList(name));
            }
            if (qun!=null){
                at.setIsAtAll(Boolean.parseBoolean(qun));
            }
            request.setAt(at);
            OapiRobotSendResponse response = client.execute(request);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
    private static String appendSign(){
        try{
            Long timestamp = System.currentTimeMillis();
            String secret = "this is secret";

            String stringToSign = timestamp + "\n" + secret;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            String sign = URLEncoder.encode(new String(Base64.getEncoder().encodeToString(signData)),"UTF-8");
            System.out.println(sign);
            return "&timestamp="+timestamp+"&sign="+sign;
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
}
